/*      */ package com.cr.common.utils.poi;
/*      */ 
/*      */ import com.cr.common.annotation.Excel;
/*      */ import com.cr.common.annotation.Excels;
/*      */ import com.cr.common.config.RuoYiConfig;
/*      */ import com.cr.common.core.domain.AjaxResult;
/*      */ import com.cr.common.core.text.Convert;
/*      */ import com.cr.common.exception.UtilException;
/*      */ import com.cr.common.utils.MyDateUtils;
/*      */ import com.cr.common.utils.DictUtils;
/*      */ import com.cr.common.utils.MyStringUtils;
/*      */ import com.cr.common.utils.file.FileTypeUtils;
/*      */ import com.cr.common.utils.file.FileUtils;
/*      */ import com.cr.common.utils.file.ImageUtils;
/*      */ import com.cr.common.utils.reflect.ReflectUtils;
/*      */ import java.io.Closeable;
/*      */ import java.io.File;
/*      */ import java.io.FileOutputStream;
/*      */ import java.io.IOException;
/*      */ import java.io.InputStream;
/*      */ import java.io.OutputStream;
/*      */ import java.lang.reflect.Field;
/*      */ import java.math.BigDecimal;
/*      */ import java.text.DecimalFormat;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Arrays;
/*      */ import java.util.Comparator;
/*      */ import java.util.Date;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Set;
/*      */ import java.util.UUID;
/*      */ import java.util.stream.Collectors;
/*      */ import javax.servlet.http.HttpServletResponse;
/*      */ import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
/*      */ import org.apache.poi.hssf.usermodel.HSSFPicture;
/*      */ import org.apache.poi.hssf.usermodel.HSSFPictureData;
/*      */ import org.apache.poi.hssf.usermodel.HSSFShape;
/*      */ import org.apache.poi.hssf.usermodel.HSSFSheet;
/*      */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*      */ import org.apache.poi.ooxml.POIXMLDocumentPart;
/*      */ import org.apache.poi.ss.usermodel.BorderStyle;
/*      */ import org.apache.poi.ss.usermodel.Cell;
/*      */ import org.apache.poi.ss.usermodel.CellStyle;
/*      */ import org.apache.poi.ss.usermodel.CellType;
/*      */ import org.apache.poi.ss.usermodel.ClientAnchor;
/*      */ import org.apache.poi.ss.usermodel.DataValidation;
/*      */ import org.apache.poi.ss.usermodel.DataValidationConstraint;
/*      */ import org.apache.poi.ss.usermodel.DataValidationHelper;
/*      */ import org.apache.poi.ss.usermodel.DateUtil;
/*      */ import org.apache.poi.ss.usermodel.Drawing;
/*      */ import org.apache.poi.ss.usermodel.FillPatternType;
/*      */ import org.apache.poi.ss.usermodel.Font;
/*      */ import org.apache.poi.ss.usermodel.HorizontalAlignment;
/*      */ import org.apache.poi.ss.usermodel.IndexedColors;
/*      */ import org.apache.poi.ss.usermodel.PictureData;
/*      */ import org.apache.poi.ss.usermodel.Row;
/*      */ import org.apache.poi.ss.usermodel.Sheet;
/*      */ import org.apache.poi.ss.usermodel.VerticalAlignment;
/*      */ import org.apache.poi.ss.usermodel.Workbook;
/*      */ import org.apache.poi.ss.usermodel.WorkbookFactory;
/*      */ import org.apache.poi.ss.util.CellRangeAddressList;
/*      */ import org.apache.poi.util.IOUtils;
/*      */ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
/*      */ import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
/*      */ import org.apache.poi.xssf.usermodel.XSSFDrawing;
/*      */ import org.apache.poi.xssf.usermodel.XSSFPicture;
/*      */ import org.apache.poi.xssf.usermodel.XSSFShape;
/*      */ import org.apache.poi.xssf.usermodel.XSSFSheet;
/*      */ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/*      */ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
/*      */ import org.slf4j.Logger;
/*      */ import org.slf4j.LoggerFactory;
/*      */ @SuppressWarnings("unchecked")
/*      */ public class ExcelUtil<T> {
/*   85 */   private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
/*      */   
/*      */   public static final int sheetSize = 65536;
/*      */   
/*      */   private String sheetName;
/*      */   
/*      */   private Excel.Type type;
/*      */   
/*      */   private Workbook wb;
/*      */   
/*      */   private Sheet sheet;
/*      */   
/*      */   private Map<String, CellStyle> styles;
/*      */   
/*      */   private List<T> list;
/*      */   
/*      */   private List<Object[]> fields;
/*      */   
/*      */   private short maxHeight;
/*      */   
/*  135 */   private Map<Integer, Double> statistics = new HashMap<>();
/*      */   
/*  140 */   private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
/*      */   
/*      */   public Class<T> clazz;
/*      */   
/*      */   public ExcelUtil(Class<T> clazz) {
/*  149 */     this.clazz = clazz;
/*      */   }
/*      */   
/*      */   public void init(List<T> list, String sheetName, Excel.Type type) {
/*  154 */     if (list == null)
/*  156 */       list = new ArrayList<>(); 
/*  158 */     this.list = list;
/*  159 */     this.sheetName = sheetName;
/*  160 */     this.type = type;
/*  161 */     createExcelField();
/*  162 */     createWorkbook();
/*      */   }
/*      */   
/*      */   public List<T> importExcel(InputStream is) throws Exception {
/*  173 */     return importExcel("", is);
/*      */   }
/*      */   
/*      */   public List<T> importExcel(String sheetName, InputStream is) throws Exception {
/*      */     Map<String, PictureData> pictures;
/*  185 */     this.type = Excel.Type.IMPORT;
/*  186 */     this.wb = WorkbookFactory.create(is);
/*  187 */     List<T> list = new ArrayList<>();
/*  189 */     Sheet sheet = MyStringUtils.isNotEmpty(sheetName) ? this.wb.getSheet(sheetName) : this.wb.getSheetAt(0);
/*  190 */     if (sheet == null)
/*  192 */       throw new IOException("文件sheet不存在"); 
/*  194 */     boolean isXSSFWorkbook = !(this.wb instanceof HSSFWorkbook);
/*  196 */     if (isXSSFWorkbook) {
/*  198 */       pictures = getSheetPictures07((XSSFSheet)sheet, (XSSFWorkbook)this.wb);
/*      */     } else {
/*  202 */       pictures = getSheetPictures03((HSSFSheet)sheet, (HSSFWorkbook)this.wb);
/*      */     } 
/*  205 */     int rows = sheet.getLastRowNum();
/*  207 */     if (rows > 0) {
/*  210 */       Map<String, Integer> cellMap = new HashMap<>();
/*  212 */       Row heard = sheet.getRow(0);
/*  213 */       for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
/*  215 */         Cell cell = heard.getCell(i);
/*  216 */         if (MyStringUtils.isNotNull(cell)) {
/*  218 */           String value = getCellValue(heard, i).toString();
/*  219 */           cellMap.put(value, Integer.valueOf(i));
/*      */         } else {
/*  223 */           cellMap.put(null, Integer.valueOf(i));
/*      */         } 
/*      */       } 
/*  227 */       Field[] allFields = this.clazz.getDeclaredFields();
/*  229 */       Map<Integer, Field> fieldsMap = new HashMap<>();
/*  230 */       for (int col = 0; col < allFields.length; col++) {
/*  232 */         Field field = allFields[col];
/*  233 */         Excel attr = field.<Excel>getAnnotation(Excel.class);
/*  234 */         if (attr != null && (attr.type() == Excel.Type.ALL || attr.type() == this.type)) {
/*  237 */           field.setAccessible(true);
/*  238 */           Integer column = cellMap.get(attr.name());
/*  239 */           if (column != null)
/*  241 */             fieldsMap.put(column, field); 
/*      */         } 
/*      */       } 
/*  245 */       for (int j = 1; j <= rows; j++) {
/*  248 */         Row row = sheet.getRow(j);
/*  250 */         if (!isRowEmpty(row)) {
/*  254 */           T entity = null;
/*  255 */           for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet()) {
/*  257 */             Object val = getCellValue(row, ((Integer)entry.getKey()).intValue());
/*  260 */             entity = (entity == null) ? this.clazz.newInstance() : entity;
/*  262 */             Field field = fieldsMap.get(entry.getKey());
/*  264 */             Class<?> fieldType = field.getType();
/*  265 */             if (String.class == fieldType) {
/*  267 */               String s = Convert.toStr(val);
/*  268 */               if (MyStringUtils.endsWith(s, ".0")) {
/*  270 */                 val = MyStringUtils.substringBefore(s, ".0");
/*      */               } else {
/*  274 */                 String dateFormat = ((Excel)field.<Excel>getAnnotation(Excel.class)).dateFormat();
/*  275 */                 if (MyStringUtils.isNotEmpty(dateFormat)) {
/*  277 */                   val = MyDateUtils.parseDateToStr(dateFormat, (Date)val);
/*      */                 } else {
/*  281 */                   val = Convert.toStr(val);
/*      */                 } 
/*      */               } 
/*  285 */             } else if ((int.class == fieldType || Integer.class == fieldType) && MyStringUtils.isNumeric(Convert.toStr(val))) {
/*  287 */               val = Convert.toInt(val);
/*  289 */             } else if (long.class == fieldType || Long.class == fieldType) {
/*  291 */               val = Convert.toLong(val);
/*  293 */             } else if (double.class == fieldType || Double.class == fieldType) {
/*  295 */               val = Convert.toDouble(val);
/*  297 */             } else if (float.class == fieldType || Float.class == fieldType) {
/*  299 */               val = Convert.toFloat(val);
/*  301 */             } else if (BigDecimal.class == fieldType) {
/*  303 */               val = Convert.toBigDecimal(val);
/*  305 */             } else if (Date.class == fieldType) {
/*  307 */               if (val instanceof String) {
/*  309 */                 val = MyDateUtils.parseDate(val);
/*  311 */               } else if (val instanceof Double) {
/*  313 */                 val = DateUtil.getJavaDate(((Double)val).doubleValue());
/*      */               } 
/*  316 */             } else if (boolean.class == fieldType || Boolean.class == fieldType) {
/*  318 */               val = Convert.toBool(val, Boolean.valueOf(false));
/*      */             } 
/*  320 */             if (MyStringUtils.isNotNull(fieldType)) {
/*  322 */               Excel attr = field.<Excel>getAnnotation(Excel.class);
/*  323 */               String propertyName = field.getName();
/*  324 */               if (MyStringUtils.isNotEmpty(attr.targetAttr())) {
/*  326 */                 propertyName = field.getName() + "." + attr.targetAttr();
/*  328 */               } else if (MyStringUtils.isNotEmpty(attr.readConverterExp())) {
/*  330 */                 val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
/*  332 */               } else if (MyStringUtils.isNotEmpty(attr.dictType())) {
/*  334 */                 val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
/*  336 */               } else if (Excel.ColumnType.IMAGE == attr.cellType() && MyStringUtils.isNotEmpty(pictures)) {
/*  338 */                 PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
/*  339 */                 if (image == null)
/*  341 */                   val = ""; 
/*  343 */                 byte[] data = image.getData();
/*  344 */                 val = FileUtils.writeImportBytes(data);
/*      */               } 
/*  346 */               ReflectUtils.invokeSetter(entity, propertyName, val);
/*      */             } 
/*      */           } 
/*  349 */           list.add(entity);
/*      */         } 
/*      */       } 
/*      */     } 
/*  352 */     return list;
/*      */   }
/*      */   
/*      */   public AjaxResult exportExcel(List<T> list, String sheetName) {
/*  364 */     init(list, sheetName, Excel.Type.EXPORT);
/*  365 */     return exportExcel();
/*      */   }
/*      */   
/*      */   public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException {
/*  379 */     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
/*  380 */     response.setCharacterEncoding("utf-8");
/*  381 */     init(list, sheetName, Excel.Type.EXPORT);
/*  382 */     exportExcel((OutputStream)response.getOutputStream());
/*      */   }
/*      */   
/*      */   public AjaxResult importTemplateExcel(String sheetName) {
/*  393 */     init(null, sheetName, Excel.Type.IMPORT);
/*  394 */     return exportExcel();
/*      */   }
/*      */   
/*      */   public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException {
/*  405 */     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
/*  406 */     response.setCharacterEncoding("utf-8");
/*  407 */     init(null, sheetName, Excel.Type.IMPORT);
/*  408 */     exportExcel((OutputStream)response.getOutputStream());
/*      */   }
/*      */   
/*      */   public void exportExcel(OutputStream out) {
/*      */     try {
/*  420 */       writeSheet();
/*  421 */       this.wb.write(out);
/*  423 */     } catch (Exception e) {
/*  425 */       log.error("导出Excel异常{}", e.getMessage());
/*      */     } finally {
/*  429 */       IOUtils.closeQuietly((Closeable)this.wb);
/*  430 */       IOUtils.closeQuietly(out);
/*      */     } 
/*      */   }
/*      */   
/*      */   public AjaxResult exportExcel() {
/*  441 */     OutputStream out = null;
/*      */     try {
/*  444 */       writeSheet();
/*  445 */       String filename = encodingFilename(this.sheetName);
/*  446 */       out = new FileOutputStream(getAbsoluteFile(filename));
/*  447 */       this.wb.write(out);
/*  448 */       return AjaxResult.success(filename);
/*  450 */     } catch (Exception e) {
/*  452 */       log.error("导出Excel异常{}", e.getMessage());
/*  453 */       throw new UtilException("导出Excel失败，请联系网站管理员！");
/*      */     } finally {
/*  457 */       IOUtils.closeQuietly((Closeable)this.wb);
/*  458 */       IOUtils.closeQuietly(out);
/*      */     } 
/*      */   }
/*      */   
/*      */   public void writeSheet() {
/*  468 */     double sheetNo = Math.ceil((this.list.size() / 65536));
/*  469 */     for (int index = 0; index <= sheetNo; index++) {
/*  471 */       createSheet(sheetNo, index);
/*  474 */       Row row = this.sheet.createRow(0);
/*  475 */       int column = 0;
/*  477 */       for (Object[] os : this.fields) {
/*  479 */         Excel excel = (Excel)os[1];
/*  480 */         createCell(excel, row, column++);
/*      */       } 
/*  482 */       if (Excel.Type.EXPORT.equals(this.type)) {
/*  484 */         fillExcelData(index, row);
/*  485 */         addStatisticsRow();
/*      */       } 
/*      */     } 
/*      */   }
/*      */   
/*      */   public void fillExcelData(int index, Row row) {
/*  498 */     int startNo = index * 65536;
/*  499 */     int endNo = Math.min(startNo + 65536, this.list.size());
/*  500 */     for (int i = startNo; i < endNo; i++) {
/*  502 */       row = this.sheet.createRow(i + 1 - startNo);
/*  504 */       T vo = this.list.get(i);
/*  505 */       int column = 0;
/*  506 */       for (Object[] os : this.fields) {
/*  508 */         Field field = (Field)os[0];
/*  509 */         Excel excel = (Excel)os[1];
/*  511 */         field.setAccessible(true);
/*  512 */         addCell(excel, row, vo, field, column++);
/*      */       } 
/*      */     } 
/*      */   }
/*      */   
/*      */   private Map<String, CellStyle> createStyles(Workbook wb) {
/*  526 */     Map<String, CellStyle> styles = new HashMap<>();
/*  527 */     CellStyle style = wb.createCellStyle();
/*  528 */     style.setAlignment(HorizontalAlignment.CENTER);
/*  529 */     style.setVerticalAlignment(VerticalAlignment.CENTER);
/*  530 */     style.setBorderRight(BorderStyle.THIN);
/*  531 */     style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
/*  532 */     style.setBorderLeft(BorderStyle.THIN);
/*  533 */     style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
/*  534 */     style.setBorderTop(BorderStyle.THIN);
/*  535 */     style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
/*  536 */     style.setBorderBottom(BorderStyle.THIN);
/*  537 */     style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
/*  538 */     Font dataFont = wb.createFont();
/*  539 */     dataFont.setFontName("Arial");
/*  540 */     dataFont.setFontHeightInPoints((short)10);
/*  541 */     style.setFont(dataFont);
/*  542 */     styles.put("data", style);
/*  544 */     style = wb.createCellStyle();
/*  545 */     style.cloneStyleFrom(styles.get("data"));
/*  546 */     style.setAlignment(HorizontalAlignment.CENTER);
/*  547 */     style.setVerticalAlignment(VerticalAlignment.CENTER);
/*  548 */     style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
/*  549 */     style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
/*  550 */     Font headerFont = wb.createFont();
/*  551 */     headerFont.setFontName("Arial");
/*  552 */     headerFont.setFontHeightInPoints((short)10);
/*  553 */     headerFont.setBold(true);
/*  554 */     headerFont.setColor(IndexedColors.WHITE.getIndex());
/*  555 */     style.setFont(headerFont);
/*  556 */     styles.put("header", style);
/*  558 */     style = wb.createCellStyle();
/*  559 */     style.setAlignment(HorizontalAlignment.CENTER);
/*  560 */     style.setVerticalAlignment(VerticalAlignment.CENTER);
/*  561 */     Font totalFont = wb.createFont();
/*  562 */     totalFont.setFontName("Arial");
/*  563 */     totalFont.setFontHeightInPoints((short)10);
/*  564 */     style.setFont(totalFont);
/*  565 */     styles.put("total", style);
/*  567 */     style = wb.createCellStyle();
/*  568 */     style.cloneStyleFrom(styles.get("data"));
/*  569 */     style.setAlignment(HorizontalAlignment.LEFT);
/*  570 */     styles.put("data1", style);
/*  572 */     style = wb.createCellStyle();
/*  573 */     style.cloneStyleFrom(styles.get("data"));
/*  574 */     style.setAlignment(HorizontalAlignment.CENTER);
/*  575 */     styles.put("data2", style);
/*  577 */     style = wb.createCellStyle();
/*  578 */     style.cloneStyleFrom(styles.get("data"));
/*  579 */     style.setAlignment(HorizontalAlignment.RIGHT);
/*  580 */     styles.put("data3", style);
/*  582 */     return styles;
/*      */   }
/*      */   
/*      */   public Cell createCell(Excel attr, Row row, int column) {
/*  591 */     Cell cell = row.createCell(column);
/*  593 */     cell.setCellValue(attr.name());
/*  594 */     setDataValidation(attr, row, column);
/*  595 */     cell.setCellStyle(this.styles.get("header"));
/*  596 */     return cell;
/*      */   }
/*      */   
/*      */   public void setCellVo(Object value, Excel attr, Cell cell) {
/*  608 */     if (Excel.ColumnType.STRING == attr.cellType()) {
/*  610 */       cell.setCellValue(MyStringUtils.isNull(value) ? attr.defaultValue() : (value + attr.suffix()));
/*  612 */     } else if (Excel.ColumnType.NUMERIC == attr.cellType()) {
/*  614 */       if (MyStringUtils.isNotNull(value))
/*  616 */         cell.setCellValue(MyStringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value).doubleValue() : Convert.toInt(value).intValue());
/*  619 */     } else if (Excel.ColumnType.IMAGE == attr.cellType()) {
/*  621 */       XSSFClientAnchor xSSFClientAnchor = new XSSFClientAnchor(0, 0, 0, 0, (short)cell.getColumnIndex(), cell.getRow().getRowNum(), (short)(cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
/*  622 */       String imagePath = Convert.toStr(value);
/*  623 */       if (MyStringUtils.isNotEmpty(imagePath)) {
/*  625 */         byte[] data = ImageUtils.getImage(imagePath);
/*  626 */         getDrawingPatriarch(cell.getSheet()).createPicture((ClientAnchor)xSSFClientAnchor, cell
/*  627 */             .getSheet().getWorkbook().addPicture(data, getImageType(data)));
/*      */       } 
/*      */     } 
/*      */   }
/*      */   
/*      */   public static Drawing<?> getDrawingPatriarch(Sheet sheet) {
/*  637 */     if (sheet.getDrawingPatriarch() == null)
/*  639 */       sheet.createDrawingPatriarch(); 
/*  641 */     return sheet.getDrawingPatriarch();
/*      */   }
/*      */   
/*      */   public int getImageType(byte[] value) {
/*  649 */     String type = FileTypeUtils.getFileExtendName(value);
/*  650 */     if ("JPG".equalsIgnoreCase(type))
/*  652 */       return 5; 
/*  654 */     if ("PNG".equalsIgnoreCase(type))
/*  656 */       return 6; 
/*  658 */     return 5;
/*      */   }
/*      */   
/*      */   public void setDataValidation(Excel attr, Row row, int column) {
/*  666 */     if (attr.name().indexOf("注：") >= 0) {
/*  668 */       this.sheet.setColumnWidth(column, 6000);
/*      */     } else {
/*  673 */       this.sheet.setColumnWidth(column, (int)((attr.width() + 0.72D) * 256.0D));
/*      */     } 
/*  676 */     if (MyStringUtils.isNotEmpty(attr.prompt()))
/*  679 */       setXSSFPrompt(this.sheet, "", attr.prompt(), 1, 100, column, column); 
/*  682 */     if ((attr.combo()).length > 0)
/*  685 */       setXSSFValidation(this.sheet, attr.combo(), 1, 100, column, column); 
/*      */   }
/*      */   
/*      */   public Cell addCell(Excel attr, Row row, T vo, Field field, int column) {
/*  694 */     Cell cell = null;
/*      */     try {
/*  698 */       row.setHeight(this.maxHeight);
/*  700 */       if (attr.isExport()) {
/*  703 */         cell = row.createCell(column);
/*  704 */         int align = attr.align().value();
/*  705 */         cell.setCellStyle(this.styles.get("data" + ((align >= 1 && align <= 3) ? Integer.valueOf(align) : "")));
/*  708 */         Object value = getTargetValue(vo, field, attr);
/*  709 */         String dateFormat = attr.dateFormat();
/*  710 */         String readConverterExp = attr.readConverterExp();
/*  711 */         String separator = attr.separator();
/*  712 */         String dictType = attr.dictType();
/*  713 */         if (MyStringUtils.isNotEmpty(dateFormat) && MyStringUtils.isNotNull(value)) {
/*  715 */           cell.setCellValue(MyDateUtils.parseDateToStr(dateFormat, (Date)value));
/*  717 */         } else if (MyStringUtils.isNotEmpty(readConverterExp) && MyStringUtils.isNotNull(value)) {
/*  719 */           cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
/*  721 */         } else if (MyStringUtils.isNotEmpty(dictType) && MyStringUtils.isNotNull(value)) {
/*  723 */           cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator));
/*  725 */         } else if (value instanceof BigDecimal && -1 != attr.scale()) {
/*  727 */           cell.setCellValue(((BigDecimal)value).setScale(attr.scale(), attr.roundingMode()).toString());
/*      */         } else {
/*  732 */           setCellVo(value, attr, cell);
/*      */         } 
/*  734 */         addStatisticsData(Integer.valueOf(column), Convert.toStr(value), attr);
/*      */       } 
/*  737 */     } catch (Exception e) {
/*  739 */       log.error("导出Excel失败{}", e);
/*      */     } 
/*  741 */     return cell;
/*      */   }
/*      */   
/*      */   public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, int firstCol, int endCol) {
/*  758 */     DataValidationHelper helper = sheet.getDataValidationHelper();
/*  759 */     DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
/*  760 */     CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
/*  761 */     DataValidation dataValidation = helper.createValidation(constraint, regions);
/*  762 */     dataValidation.createPromptBox(promptTitle, promptContent);
/*  763 */     dataValidation.setShowPromptBox(true);
/*  764 */     sheet.addValidationData(dataValidation);
/*      */   }
/*      */   
/*      */   public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
/*  780 */     DataValidationHelper helper = sheet.getDataValidationHelper();
/*  782 */     DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
/*  784 */     CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
/*  786 */     DataValidation dataValidation = helper.createValidation(constraint, regions);
/*  788 */     if (dataValidation instanceof org.apache.poi.xssf.usermodel.XSSFDataValidation) {
/*  790 */       dataValidation.setSuppressDropDownArrow(true);
/*  791 */       dataValidation.setShowErrorBox(true);
/*      */     } else {
/*  795 */       dataValidation.setSuppressDropDownArrow(false);
/*      */     } 
/*  798 */     sheet.addValidationData(dataValidation);
/*      */   }
/*      */   
/*      */   public static String convertByExp(String propertyValue, String converterExp, String separator) {
/*  811 */     StringBuilder propertyString = new StringBuilder();
/*  812 */     String[] convertSource = converterExp.split(",");
/*  813 */     for (String item : convertSource) {
/*  815 */       String[] itemArray = item.split("=");
/*  816 */       if (MyStringUtils.containsAny(separator, propertyValue)) {
/*  818 */         for (String value : propertyValue.split(separator)) {
/*  820 */           if (itemArray[0].equals(value)) {
/*  822 */             propertyString.append(itemArray[1] + separator);
/*      */             break;
/*      */           } 
/*      */         } 
/*  829 */       } else if (itemArray[0].equals(propertyValue)) {
/*  831 */         return itemArray[1];
/*      */       } 
/*      */     } 
/*  835 */     return MyStringUtils.stripEnd(propertyString.toString(), separator);
/*      */   }
/*      */   
/*      */   public static String reverseByExp(String propertyValue, String converterExp, String separator) {
/*  848 */     StringBuilder propertyString = new StringBuilder();
/*  849 */     String[] convertSource = converterExp.split(",");
/*  850 */     for (String item : convertSource) {
/*  852 */       String[] itemArray = item.split("=");
/*  853 */       if (MyStringUtils.containsAny(separator, propertyValue)) {
/*  855 */         for (String value : propertyValue.split(separator)) {
/*  857 */           if (itemArray[1].equals(value)) {
/*  859 */             propertyString.append(itemArray[0] + separator);
/*      */             break;
/*      */           } 
/*      */         } 
/*  866 */       } else if (itemArray[1].equals(propertyValue)) {
/*  868 */         return itemArray[0];
/*      */       } 
/*      */     } 
/*  872 */     return MyStringUtils.stripEnd(propertyString.toString(), separator);
/*      */   }
/*      */   
/*      */   public static String convertDictByExp(String dictValue, String dictType, String separator) {
/*  885 */     return DictUtils.getDictLabel(dictType, dictValue, separator);
/*      */   }
/*      */   
/*      */   public static String reverseDictByExp(String dictLabel, String dictType, String separator) {
/*  898 */     return DictUtils.getDictValue(dictType, dictLabel, separator);
/*      */   }
/*      */   
/*      */   private void addStatisticsData(Integer index, String text, Excel entity) {
/*  906 */     if (entity != null && entity.isStatistics()) {
/*  908 */       Double temp = Double.valueOf(0.0D);
/*  909 */       if (!this.statistics.containsKey(index))
/*  911 */         this.statistics.put(index, temp); 
/*      */       try {
/*  915 */         temp = Double.valueOf(text);
/*  917 */       } catch (NumberFormatException numberFormatException) {}
/*  920 */       this.statistics.put(index, Double.valueOf(((Double)this.statistics.get(index)).doubleValue() + temp.doubleValue()));
/*      */     } 
/*      */   }
/*      */   
/*      */   public void addStatisticsRow() {
/*  929 */     if (this.statistics.size() > 0) {
/*  931 */       Row row = this.sheet.createRow(this.sheet.getLastRowNum() + 1);
/*  932 */       Set<Integer> keys = this.statistics.keySet();
/*  933 */       Cell cell = row.createCell(0);
/*  934 */       cell.setCellStyle(this.styles.get("total"));
/*  935 */       cell.setCellValue("合计");
/*  937 */       for (Integer key : keys) {
/*  939 */         cell = row.createCell(key.intValue());
/*  940 */         cell.setCellStyle(this.styles.get("total"));
/*  941 */         cell.setCellValue(DOUBLE_FORMAT.format(this.statistics.get(key)));
/*      */       } 
/*  943 */       this.statistics.clear();
/*      */     } 
/*      */   }
/*      */   
/*      */   public String encodingFilename(String filename) {
/*  952 */     filename = UUID.randomUUID().toString() + "_" + filename + ".xlsx";
/*  953 */     return filename;
/*      */   }
/*      */   
/*      */   public String getAbsoluteFile(String filename) {
/*  963 */     String downloadPath = RuoYiConfig.getDownloadPath() + filename;
/*  964 */     File desc = new File(downloadPath);
/*  965 */     if (!desc.getParentFile().exists())
/*  967 */       desc.getParentFile().mkdirs(); 
/*  969 */     return downloadPath;
/*      */   }
/*      */   
/*      */   private Object getTargetValue(T vo, Field field, Excel excel) throws Exception {
/*  983 */     Object o = field.get(vo);
/*  984 */     if (MyStringUtils.isNotEmpty(excel.targetAttr())) {
/*  986 */       String target = excel.targetAttr();
/*  987 */       if (target.indexOf(".") > -1) {
/*  989 */         String[] targets = target.split("[.]");
/*  990 */         for (String name : targets)
/*  992 */           o = getValue(o, name); 
/*      */       } else {
/*  997 */         o = getValue(o, target);
/*      */       } 
/*      */     } 
/* 1000 */     return o;
/*      */   }
/*      */   
/*      */   private Object getValue(Object o, String name) throws Exception {
/* 1013 */     if (MyStringUtils.isNotNull(o) && MyStringUtils.isNotEmpty(name)) {
/* 1015 */       Class<?> clazz = o.getClass();
/* 1016 */       Field field = clazz.getDeclaredField(name);
/* 1017 */       field.setAccessible(true);
/* 1018 */       o = field.get(o);
/*      */     } 
/* 1020 */     return o;
/*      */   }
/*      */   
/*      */   private void createExcelField() {
/* 1028 */     this.fields = new ArrayList();
/* 1029 */     List<Field> tempFields = new ArrayList<>();
/* 1030 */     tempFields.addAll(Arrays.asList(this.clazz.getSuperclass().getDeclaredFields()));
/* 1031 */     tempFields.addAll(Arrays.asList(this.clazz.getDeclaredFields()));
/* 1032 */     for (Field field : tempFields) {
/* 1035 */       if (field.isAnnotationPresent((Class)Excel.class))
/* 1037 */         putToField(field, field.<Excel>getAnnotation(Excel.class)); 
/* 1041 */       if (field.isAnnotationPresent((Class)Excels.class)) {
/* 1043 */         Excels attrs = field.<Excels>getAnnotation(Excels.class);
/* 1044 */         Excel[] excels = attrs.value();
/* 1045 */         for (Excel excel : excels)
/* 1047 */           putToField(field, excel); 
/*      */       } 
/*      */     } 
/* 1051 */     this.fields = (List<Object[]>)this.fields.stream().sorted(Comparator.comparing(objects -> Integer.valueOf(((Excel)objects[1]).sort()))).collect(Collectors.toList());
/* 1052 */     this.maxHeight = getRowHeight();
/*      */   }
/*      */   
/*      */   public short getRowHeight() {
/* 1060 */     double maxHeight = 0.0D;
/* 1061 */     for (Object[] os : this.fields) {
/* 1063 */       Excel excel = (Excel)os[1];
/* 1064 */       maxHeight = (maxHeight > excel.height()) ? maxHeight : excel.height();
/*      */     } 
/* 1066 */     return (short)(int)(maxHeight * 20.0D);
/*      */   }
/*      */   
/*      */   private void putToField(Field field, Excel attr) {
/* 1074 */     if (attr != null && (attr.type() == Excel.Type.ALL || attr.type() == this.type))
/* 1076 */       this.fields.add(new Object[] { field, attr }); 
/*      */   }
/*      */   
/*      */   public void createWorkbook() {
/* 1085 */     this.wb = (Workbook)new SXSSFWorkbook(500);
/*      */   }
/*      */   
/*      */   public void createSheet(double sheetNo, int index) {
/* 1096 */     this.sheet = this.wb.createSheet();
/* 1097 */     this.styles = createStyles(this.wb);
/* 1099 */     if (sheetNo == 0.0D) {
/* 1101 */       this.wb.setSheetName(index, this.sheetName);
/*      */     } else {
/* 1105 */       this.wb.setSheetName(index, this.sheetName + index);
/*      */     } 
/*      */   }
/*      */   
/*      */   public Object getCellValue(Row row, int column) {
/* 1118 */     if (row == null)
/* 1120 */       return row; 
/* 1122 */     Object val = "";
/*      */     try {
/* 1125 */       Cell cell = row.getCell(column);
/* 1126 */       if (MyStringUtils.isNotNull(cell))
/* 1128 */         if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA) {
/* 1130 */           val = Double.valueOf(cell.getNumericCellValue());
/* 1131 */           if (DateUtil.isCellDateFormatted(cell)) {
/* 1133 */             val = DateUtil.getJavaDate(((Double)val).doubleValue());
/* 1137 */           } else if (((Double)val).doubleValue() % 1.0D != 0.0D) {
/* 1139 */             val = new BigDecimal(val.toString());
/*      */           } else {
/* 1143 */             val = (new DecimalFormat("0")).format(val);
/*      */           } 
/* 1147 */         } else if (cell.getCellType() == CellType.STRING) {
/* 1149 */           val = cell.getStringCellValue();
/* 1151 */         } else if (cell.getCellType() == CellType.BOOLEAN) {
/* 1153 */           val = Boolean.valueOf(cell.getBooleanCellValue());
/* 1155 */         } else if (cell.getCellType() == CellType.ERROR) {
/* 1157 */           val = Byte.valueOf(cell.getErrorCellValue());
/*      */         }  
/* 1162 */     } catch (Exception e) {
/* 1164 */       return val;
/*      */     } 
/* 1166 */     return val;
/*      */   }
/*      */   
/*      */   private boolean isRowEmpty(Row row) {
/* 1177 */     if (row == null)
/* 1179 */       return true; 
/* 1181 */     for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
/* 1183 */       Cell cell = row.getCell(i);
/* 1184 */       if (cell != null && cell.getCellType() != CellType.BLANK)
/* 1186 */         return false; 
/*      */     } 
/* 1189 */     return true;
/*      */   }
/*      */   
/*      */   public static Map<String, PictureData> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook) {
/* 1201 */     Map<String, PictureData> sheetIndexPicMap = new HashMap<>();
/* 1202 */     List<HSSFPictureData> pictures = workbook.getAllPictures();
/* 1203 */     if (!pictures.isEmpty()) {
/* 1205 */       for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
/* 1207 */         HSSFClientAnchor anchor = (HSSFClientAnchor)shape.getAnchor();
/* 1208 */         if (shape instanceof HSSFPicture) {
/* 1210 */           HSSFPicture pic = (HSSFPicture)shape;
/* 1211 */           int pictureIndex = pic.getPictureIndex() - 1;
/* 1212 */           HSSFPictureData picData = pictures.get(pictureIndex);
/* 1213 */           String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());
/* 1214 */           sheetIndexPicMap.put(picIndex, picData);
/*      */         } 
/*      */       } 
/* 1217 */       return sheetIndexPicMap;
/*      */     } 
/* 1221 */     return sheetIndexPicMap;
/*      */   }
/*      */   
/*      */   public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFWorkbook workbook) {
/* 1234 */     Map<String, PictureData> sheetIndexPicMap = new HashMap<>();
/* 1235 */     for (POIXMLDocumentPart dr : sheet.getRelations()) {
/* 1237 */       if (dr instanceof XSSFDrawing) {
/* 1239 */         XSSFDrawing drawing = (XSSFDrawing)dr;
/* 1240 */         List<XSSFShape> shapes = drawing.getShapes();
/* 1241 */         for (XSSFShape shape : shapes) {
/* 1243 */           if (shape instanceof XSSFPicture) {
/* 1245 */             XSSFPicture pic = (XSSFPicture)shape;
/* 1246 */             XSSFClientAnchor anchor = pic.getPreferredSize();
/* 1247 */             CTMarker ctMarker = anchor.getFrom();
/* 1248 */             String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
/* 1249 */             sheetIndexPicMap.put(picIndex, pic.getPictureData());
/*      */           } 
/*      */         } 
/*      */       } 
/*      */     } 
/* 1254 */     return sheetIndexPicMap;
/*      */   }
/*      */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-common-4.7.0.jar!\com\cr\commo\\utils\poi\ExcelUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */